package com.moleq.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.moleq.models.MixMatch_detail;
import com.moleq.mgpos.activities.R;

public class MixMatchSelectionAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<MixMatch_detail> mmlist;
	public ArrayList<CheckBox> cbs = new ArrayList<CheckBox>();
	public boolean lb_check;
	private int selectedRow = 0;

	public MixMatchSelectionAdapter(Context context, List<MixMatch_detail> mmlist) {
		this.mInflater = LayoutInflater.from(context);
		this.mmlist = mmlist;

	}

	public void setSource(List<MixMatch_detail> mmlist) {
		this.mmlist = mmlist;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mmlist.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mmlist.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parents) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		String ms_no, name;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.mm_detail_select_item, null);
			holder.tv_item = (TextView) convertView.findViewById(R.id.tv_ms_no);
			holder.tv_des = (TextView) convertView.findViewById(R.id.tv_ms_desc);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		ms_no = String.valueOf(mmlist.get(position).getMs_no());
		name = mmlist.get(position).getMs_desc();

		holder.tv_item.setText(String.valueOf(ms_no));
		if (name.length() > 17) {
			holder.tv_des.setText(String.valueOf(name.substring(0, 17)));
		} else {
			holder.tv_des.setText(String.valueOf(name));
		}

		if (position == selectedRow) {
			holder.tv_item.setBackgroundColor(Color.rgb(255, 219, 16));
			holder.tv_des.setBackgroundColor(Color.rgb(255, 219, 16));
		} else {
			holder.tv_item.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.tv_des.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		return convertView;
	}

	public final class ViewHolder {
		public TextView tv_item;
		public TextView tv_des;
	}

	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}

	public int GetSelectedIndex() {
		return selectedRow;
	}
}
